﻿using System;
using System.Linq;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.Security;
using LMHTX.Modules.DIRECTORY.COOPERATIVE.Components;
using System.Collections.Generic;
using System.Web;
using Type=LMHTX.Modules.DIRECTORY.COOPERATIVE.Components.Type;

namespace LMHTX.Modules.DIRECTORY.COOPERATIVE.Controls
{
    public partial class AddEditItem : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += AddEditItem_Update;
            this.Delete += AddEditItem_Delete;
            this.Cancel += AddEditItem_Cancel;
            base.OnInit(e);
        }

        void AddEditItem_Cancel(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "CooperativeManager");
            Response.Redirect(url);
        }

        void AddEditItem_Delete(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                Cooperative.DeleteCooperative(this.ItemID);
                this.ReturnToReferrerPage();
            }
        }

        void AddEditItem_Update(object sender, EventArgs e)
        {

            Cooperative cooperative = new Cooperative();
            if (this.ItemID > 0)
            {
                cooperative = Cooperative.Load(this.ItemID);
            }

            cooperative.ModuleID = this.ModuleID;
            cooperative.Title = txtTitle.Text;
            cooperative.AreaID = Convert.ToInt32(ddlArea.SelectedValue);
            cooperative.TypeID = Convert.ToInt32(ddlType.SelectedValue);
            cooperative.CareerID = Convert.ToInt32(ddlCareer.SelectedValue);

            cooperative.ChairmanID = Convert.ToInt32(ddlChairman.SelectedValue);
            cooperative.ViceChairmanID = Convert.ToInt32(ddlViceChairman.SelectedValue);
            cooperative.ChiefAccountantID = Convert.ToInt32(ddlChiefAccountant.SelectedValue);
            cooperative.ControllerID = Convert.ToInt32(ddlController.SelectedValue);

            cooperative.Email = txtEmail.Text;
            cooperative.Address = txtAddress.Text;
            cooperative.Website = txtWebsite.Text;
            cooperative.Phone = txtPhone.Text;
            cooperative.Fax = txtFax.Text;

            if (txtEstablishedYear.Text.Trim().Length != 0)
            {
                cooperative.EstablishedYear = Convert.ToInt32(txtEstablishedYear.Text);
            }

            cooperative.LicenseNumber = txtLicense.Text;

           
            // Thị trường
            cooperative.MarketDescription = txtMarketDescription.Text;
            cooperative.HasInlandMarket = chkHasInlandMarket.Checked;
            cooperative.HasOutlandMarket = chkHasOutlandMarket.Checked;

            // Sản phẩm / Dịch vụ
            cooperative.ProductDescription = HttpUtility.HtmlEncode(txtProduct.Value);
            
            // Tổ chức / Đoàn thể
            cooperative.OrganizationDescription = txtOrganizationDescription.Text;
            cooperative.HasPartyOrganization = chkHasPartyOrganization.Checked;
            cooperative.HasTradeUnion = chkHasTradeUnion.Checked;
            cooperative.HasTheCommunistYouthLeague = chkHasTheCommunistYouthLeague.Checked;
            cooperative.HasWomanUnion = chkHasWomanUnion.Checked;

            cooperative.Description = txtDescription.Text;

            cooperative.CreatedDate = DateTime.Now;
            cooperative.CreatedBy = PortalSecurity.CurrentUser.Identity.Email;

            if (this.ItemID == 0)
            {
                cooperative.CreatedDate = DateTime.Now;
                this.ItemID = cooperative.Insert();
            }
            else
            {
                cooperative.CreatedDate = cooperative.CreatedDate;
                cooperative.ID = this.ItemID;
                cooperative.Update();
            }

            string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, this.ItemID, "AddEditHistory");
            Response.Redirect(url);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                IList<Employee> employees = Employee.SelectCollectionAll(this.ModuleID);
                
                ddlChairman.DataSource = employees.Where(x => x.JobTitle == "CN").OrderBy(x => x.FullName);
                ddlChairman.DataBind();

                ddlViceChairman.DataSource = employees.Where(x => x.JobTitle == "PCN").OrderBy(x => x.FullName);
                ddlViceChairman.DataBind();

                ddlChiefAccountant.DataSource = employees.Where(x => x.JobTitle == "KTT").OrderBy(x => x.FullName);
                ddlChiefAccountant.DataBind();

                ddlController.DataSource = employees.Where(x => x.JobTitle == "TBKS").OrderBy(x => x.FullName);
                ddlController.DataBind();

                ddlArea.DataSource = Area.SelectAll(this.ModuleID);
                ddlArea.DataBind();
                
                ddlType.DataSource = Type.SelectAll(this.ModuleID);
                ddlType.DataBind();

                ddlCareer.DataSource = Career.SelectAll(this.ModuleID);
                ddlCareer.DataBind();

                if (this.ItemID > 0)
                {
                    Cooperative cooperative = Cooperative.Load(this.ItemID);
                    if (cooperative != null)
                    {
                        ddlArea.SelectedValue = cooperative.AreaID.ToString();
                        ddlType.SelectedValue = cooperative.TypeID.ToString();
                        ddlCareer.SelectedValue = cooperative.CareerID.ToString();

                        txtTitle.Text = cooperative.Title;
                        ddlChairman.SelectedValue = cooperative.ChairmanID.ToString();
                        ddlViceChairman.SelectedValue = cooperative.ViceChairmanID.ToString();
                        ddlChiefAccountant.SelectedValue = cooperative.ChiefAccountantID.ToString();
                        ddlController.SelectedValue = cooperative.ControllerID.ToString();
                        txtEmail.Text = cooperative.Email;
                        txtWebsite.Text = cooperative.Website;
                        txtAddress.Text = cooperative.Address;
                        txtPhone.Text = cooperative.Phone;
                        txtFax.Text = cooperative.Fax;
                        txtEstablishedYear.Text = cooperative.EstablishedYear.ToString();
                        txtLicense.Text = cooperative.LicenseNumber;
                        
                        
                        txtMarketDescription.Text = cooperative.MarketDescription;
                        chkHasInlandMarket.Checked = cooperative.HasInlandMarket;
                        chkHasOutlandMarket.Checked = cooperative.HasOutlandMarket;

                        // Tổ chức / Đoàn thể
                        txtOrganizationDescription.Text = cooperative.OrganizationDescription;
                        chkHasPartyOrganization.Checked = cooperative.HasPartyOrganization;
                        chkHasTradeUnion.Checked = cooperative.HasTradeUnion;
                        chkHasTheCommunistYouthLeague.Checked = cooperative.HasTheCommunistYouthLeague;
                        chkHasWomanUnion.Checked = cooperative.HasWomanUnion;

                        txtProduct.Value = HttpUtility.HtmlDecode(cooperative.ProductDescription);
                        txtDescription.Text = cooperative.Description;
                    }
                }
                else
                {
                    btnDelete.Visible = false;
                }
            }
        }
    }
}